package internal

import (
	"github.com/name5566/leaf/gate"
	"reflect"
	"server/msg"
	"github.com/name5566/leaf/log"
)

func init() {
	handler(&msg.JoinRoom{}, handleJoinRoom)
	handler(&msg.MoveRot{}, handleMoveRot)
	handler(&msg.MovePos{}, handleMovePos)
	handler(&msg.MoveStart{}, handleMoveStart)
	handler(&msg.MoveInput{}, handleMoveInput)
}

func handler(m interface{}, h interface{}) {
	skeleton.RegisterChanRPC(reflect.TypeOf(m), h)
}

func handleMoveInput(args []interface{}) {
	m := args[0].(*msg.MoveInput)
	a := args[1].(gate.Agent)

	if a == nil {
		log.Error("dont have client")
		return
	}

	room := playerID2Player[m.PlayerId].room

	if room == nil {
		return
	}

	for _, p := range room.roomPlayers {
		p.WriteMsg(&msg.MoveInput{
			PlayerId: m.PlayerId,
			Input : m.Input,
		})
	}
}

func handleMovePos(args []interface{}) {
	m := args[0].(*msg.MovePos)
	a := args[1].(gate.Agent)

	if a == nil {
		log.Error("dont have client")
		return
	}

	ap := playerID2Player[m.PlayerId]

	ap.position = msg.Vector3{
		X:m.Positon.X,
		Y:m.Positon.Y,
		Z:m.Positon.Z,
	}

	room := playerID2Player[m.PlayerId].room

	if room == nil {
		log.Error("room nil")
		return
	}

	for _, p := range room.roomPlayers {
		p.WriteMsg(&msg.MovePos{
			PlayerId: m.PlayerId,
			Positon: m.Positon,
		})
	}
}

func handleMoveRot(args []interface{}) {
	m := args[0].(*msg.MoveRot)
	a := args[1].(gate.Agent)

	if a == nil {
		log.Error("dont have client")
		return
	}

	playerID2Player[m.PlayerId].rotation = msg.Vector3{
		X: m.Rotation.X,
		Y: m.Rotation.Y,
		Z: m.Rotation.Z,
	}

	room := playerID2Player[m.PlayerId].room

	if room == nil {
		log.Error("room nil")
		return
	}

	for _, p := range room.roomPlayers {
		p.WriteMsg(&msg.MoveRot{
			PlayerId: m.PlayerId,
			Rotation: m.Rotation,
		})
	}
}

func handleMoveStart(args []interface{}) {
	m := args[0].(*msg.MoveStart)
	a := args[1].(gate.Agent)

	if a == nil {
		log.Error("dont have client")
		return
	}

	room := playerID2Player[m.PlayerId].room

	if room == nil {
		log.Error("room nil")
		return
	}

	for _, p := range room.roomPlayers {
		p.WriteMsg(&msg.MoveStart{
			PlayerId: m.PlayerId,
			IsMove: m.IsMove,
		})
	}
}

func handleJoinRoom(args []interface{}) {
	m := args[0].(*msg.JoinRoom)
	a := args[1].(gate.Agent)

	AddPlayer(a, m.PlayerId, m.Type)

	var playerData msg.PlayerData
	room := playerID2Player[m.PlayerId].room

	if room == nil {
		return
	}

	//向加入房间的玩家发送游戏数据
	for key, p := range room.roomPlayers {
		if p != a {
			player:= playerID2Player[key]
			if player == nil {
				log.Error("player nil " , key)
				return
			}

			playerData.Players = append(playerData.Players, &msg.PlayerPos{
					PlayerId : key,
					Position : &msg.Vector3{
						X: player.position.X,
						Y: player.position.Y,
						Z: player.position.Z,
					},
					Rotation: &msg.Vector3{
						X: player.rotation.X,
						Y: player.rotation.Y,
						Z: player.rotation.Z,
					},
			})
		}
	}

	a.WriteMsg(&msg.PlayerData{
			Players: playerData.Players,
	})

	//向房间内的玩家广播加入消息
	for _, p := range room.roomPlayers {
		if p !=a {
			p.WriteMsg(&msg.JoinRoom{
				PlayerId: m.PlayerId,
			})
		}
	}
}
